package org.edu.modules.growth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.edu.modules.growth.config.QueryConditionTool;
import org.edu.modules.growth.config.TrainLevelEnum;
import org.edu.modules.growth.config.TrainTypeEnum;
import org.edu.modules.growth.entity.VeJsczTraining;
import org.edu.modules.growth.mapper.IVeJsczTraingMapper;
import org.edu.modules.growth.service.IVeJsczTrainingService;
import org.edu.modules.growth.vo.DictVo;
import org.edu.modules.growth.vo.QueryCondition;
import org.edu.modules.growth.vo.TrainingEnumTypeVo;
import org.edu.modules.growth.vo.VeJsczTrainingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class VeJsczTrainingServiceImpl extends ServiceImpl<IVeJsczTraingMapper,
        VeJsczTraining> implements IVeJsczTrainingService {


    private IVeJsczTraingMapper mIVeJsczTraingMapper;

    @Override
    public Map<String,Object> selectPage(QueryCondition queryCondition ){
        HashMap<String,Object> map = new HashMap<>();
        IPage<VeJsczTraining> page = new Page<>(queryCondition.getCurrent(), queryCondition.getPageSize());
        QueryWrapper<VeJsczTraining> wrapper = new QueryWrapper<>() ;
        QueryConditionTool.advancedQueryPage(queryCondition.getConditions(),wrapper);
        wrapper.orderByDesc("id");
        page = mIVeJsczTraingMapper.selectPage(page,wrapper);
        //获取数据
        List<VeJsczTraining> list = page.getRecords();
        //统计页码
        Integer count = mIVeJsczTraingMapper.selectCount(wrapper);
        List<VeJsczTrainingVo> res = Lists.newArrayList() ;
        if( !CollectionUtils.isEmpty(list)){
            list.forEach(l->{
                VeJsczTrainingVo vo = new VeJsczTrainingVo() ;
                BeanUtils.copyProperties(l,vo);
                vo.setTrainStyleName(TrainTypeEnum.getNameByValue(l.getTrainStyle())) ;
                vo.setTrainLevelName(TrainLevelEnum.getNameByValue(l.getTrainLevel())) ;
                res.add(vo) ;
            });
        }
        map.put("list",res);
        map.put("count", count);
        return map;
    }

    @Override
    public TrainingEnumTypeVo getEnumType() {
        List<DictVo> types = Lists.newArrayList() ;
        for (TrainTypeEnum v : TrainTypeEnum.values()) {
            types.add(new DictVo()
                    .setKey(v.getTypeVal())
                    .setValue(v.getTypeName()));
        }
        List<DictVo> levels = Lists.newArrayList() ;
        for (TrainLevelEnum v : TrainLevelEnum.values()) {
            levels.add(new DictVo()
                    .setKey(v.getTypeVal())
                    .setValue(v.getTypeName()));
        }
        return new TrainingEnumTypeVo().setTrainType(types).setTrainLevel(levels) ;
    }


}
